package com.cts.controller.admin;

import com.cts.annotation.OperationLogger;
import com.cts.entity.Moment;
import com.cts.model.dto.MomentDTO;
import com.cts.model.vo.MomentVO;
import com.cts.model.vo.PageResultVO;
import com.cts.model.vo.Result;
import com.cts.service.MomentService;
import com.cts.util.DateFormatUtils;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @projectName: CBlog
 * @package: com.cts.controller.admin
 * @className: MomentAdminController
 * @author: chentongsheng
 * @description: TODO
 * @date: 2023/2/23 21:49
 * @version: 1.0
 */
@RestController
@RequestMapping("/admin")
public class MomentAdminController {

    @Autowired
    MomentService momentService;

    @Autowired
    DateFormatUtils dateFormatUtils;

    @OperationLogger("获取动态列表")
    @ApiOperation("获取动态列表")
    @PostMapping("/getMomentList")
    public Result getMomentList(@RequestBody MomentDTO momentDTO){
        PageResultVO<Moment> pageResult = momentService.getMomentList(momentDTO);
        return Result.ok("请求成功", pageResult);
    }

    @OperationLogger("发布动态")
    @ApiOperation("发布动态")
    @PostMapping("/publishMoment")
    public Result publishMoment(@RequestParam Integer id, Boolean isPublished){
        Boolean result = momentService.publishMoment(id, isPublished);
        if(result){
            return Result.ok("发布成功");
        }
        return Result.error("发布失败");
    }

    @OperationLogger("更新动态")
    @ApiOperation("更新动态")
    @PostMapping("/saveMoment")
    public Result saveMoment(@RequestBody MomentDTO momentDTO){
        Moment moment = new Moment();
        moment.setId(momentDTO.getId());
        moment.setContent(momentDTO.getContent());
        moment.setLikes(momentDTO.getLikes() == null ? 0 : momentDTO.getLikes());
        moment.setIsPublished(momentDTO.getIsPublished() == null ? false : true);
        if(StringUtil.isNullOrEmpty(momentDTO.getCreateTime())) {
            moment.setCreateTime(dateFormatUtils.getNow());
        }else{
            moment.setCreateTime(dateFormatUtils.StringToLocalDateTime(momentDTO.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
        }

        Boolean result = momentService.saveOrUpdate(moment);

        if(result){
            return Result.ok("保存成功");
        }
        return Result.error("保存失败");
    }

    @OperationLogger("删除动态")
    @ApiOperation("删除动态")
    @PostMapping("/deleteMoment")
    public Result deleteMoment(@RequestParam Integer id){
        Boolean result = momentService.removeById(id);
        if(result){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    @OperationLogger("获取动态")
    @ApiOperation("获取动态")
    @GetMapping("/getMoment")
    public Result getMoment(@RequestParam Integer id){
        Moment moment = momentService.getById(id);
        MomentVO momentVO = new MomentVO();
        momentVO.setId(moment.getId());
        momentVO.setContent(moment.getContent());
        momentVO.setLikes(moment.getLikes());
        momentVO.setCreateTime(dateFormatUtils.LocalDateTimeToString(moment.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));
        momentVO.setIsPublished(moment.getIsPublished());
        System.out.println(momentVO);
        return Result.ok("删除成功", momentVO);
    }


}
